package com.example.zx.theline.zsjh.pub;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zx.theline.R;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import GsonBean.CheckInfoBean;
import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import GsonBean.ZsjhChangeBean;
import GsonBean.ZsyjhxxBean;
import Utils.MyToast;
import Utils.StatusUtil;
import app.MyApplication;
import base.BaseActivity;
import dialog.CenterDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 正式日计划查看activity
 */
public class ZsrjhChaKanActivity extends BaseActivity implements View.OnClickListener {

    public static CheckInfoBean checkInfoBean;
    private ShyjhxqBean mZsyjhxxBean;

    /**
     * 标题
     */
    TextView centerTextView;
    /**
     * 计划内容
     */
    TextView lb;
    TextView rjhh;
    TextView ydbh;
    TextView rqsj;
    TextView kssj;
    TextView jssj;
    TextView xm;
    TextView dd;
    TextView hb;
    TextView lc;
    TextView xmmc;
    TextView sgnr;
    TextView sgjx;
    TextView blfh;
    TextView aqfx;
    TextView fxcs;

    /**
     * 相关单位
     */
    TextView sgdw;
    TextView sgdwfzr;
    TextView jldw;
    TextView jldwfzr;
    TextView jsdw;
    TextView cwzd;
    TextView ddt;
    TextView dxjz;
    TextView sbgldw;
    TextView qtdw;
    TextView shcs;
    TextView xmzgbm;

    /**
     * 按钮
     */
    TextView cancel;
    TextView ckyjh;
    TextView zf;
    TextView hf;
    TextView leftTextView;

    //允许恢复
    boolean isRecovery = false;
    //允许废除
    boolean isDesorty = false;

    ZsjhChangeBean mZsjhChangeBean;
    private LinearLayout blfh_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zsrjh_cha_kan);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {

        centerTextView = (TextView) findViewById(R.id.centerTextView);
        centerTextView.setText("日计划详情");

        lb = (TextView) findViewById(R.id.lb);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbh = (TextView) findViewById(R.id.ydbh);
        rqsj = (TextView) findViewById(R.id.rqsj);
        kssj = (TextView) findViewById(R.id.kssj);
        jssj = (TextView) findViewById(R.id.jssj);
        xm = (TextView) findViewById(R.id.xm);
        dd = (TextView) findViewById(R.id.dd);
        hb = (TextView) findViewById(R.id.hb);
        lc = (TextView) findViewById(R.id.lc);
        xmmc = (TextView) findViewById(R.id.xmmc);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);
        blfh_layout = (LinearLayout) findViewById(R.id.blfh_layout);
        aqfx = (TextView) findViewById(R.id.aqfx);
        fxcs = (TextView) findViewById(R.id.fxcs);

        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdwfzr = (TextView) findViewById(R.id.sgdwfzr);
        jldw = (TextView) findViewById(R.id.jldw);
        jldwfzr = (TextView) findViewById(R.id.jldwfzr);
        jsdw = (TextView) findViewById(R.id.jsdw);
        cwzd = (TextView) findViewById(R.id.cwzd);
        ddt = (TextView) findViewById(R.id.ddt);
        dxjz = (TextView) findViewById(R.id.dxjz);
        sbgldw = (TextView) findViewById(R.id.sbgldw);
        qtdw = (TextView) findViewById(R.id.qtdw);
        shcs = (TextView) findViewById(R.id.shcs);
        xmzgbm = (TextView) findViewById(R.id.xmzgbm);

        cancel = (TextView) findViewById(R.id.cancel);
        ckyjh = (TextView) findViewById(R.id.ckyjh);
        hf = (TextView) findViewById(R.id.hf);
        zf = (TextView) findViewById(R.id.zf);
        leftTextView = (TextView) findViewById(R.id.leftTextView);

    }

    @Override
    protected void InitData() {
        if (checkInfoBean == null) {
            MyToast.showFail();
        } else {
            lb.setText(checkInfoBean.getInfo().getLb());
            rjhh.setText(checkInfoBean.getInfo().getBh());
            ydbh.setText(checkInfoBean.getInfo().getYdbh());
            rqsj.setText(checkInfoBean.getInfo().getSgrq());
            kssj.setText(checkInfoBean.getInfo().getSgsjks());
            jssj.setText(checkInfoBean.getInfo().getSgsjjs());
            xm.setText(checkInfoBean.getInfo().getXlxbList().get(0).getXlmc());
            dd.setText(checkInfoBean.getInfo().getXlxbList().get(0).getSgdd());
            hb.setText(checkInfoBean.getInfo().getXlxbList().get(0).getXbmc());
            lc.setText(checkInfoBean.getInfo().getXlxbList().get(0).getLic());
            xmmc.setText(checkInfoBean.getInfo().getXmmc());
            sgnr.setText(checkInfoBean.getInfo().getNrmc());
            sgjx.setText(checkInfoBean.getInfo().getJxmc());
//            blfh.setText(checkInfoBean.getInfo().getBlfhcsCombine());
            if (checkInfoBean.getInfo().getLb().equals("C")) { //B类防护
                blfh_layout.setVisibility(View.GONE);
            } else {
                blfh_layout.setVisibility(View.VISIBLE);
                blfh.setText(checkInfoBean.getInfo().getBlfhmc());
            }
            aqfx.setText(checkInfoBean.getInfo().getAqfxmc());
            fxcs.setText(checkInfoBean.getInfo().getAqfxcsmc());

            sgdw.setText(checkInfoBean.getInfo().getSgdw());
            sgdwfzr.setText(checkInfoBean.getInfo().getSgdwfzr());
            jldw.setText(checkInfoBean.getInfo().getJldw());
            jldwfzr.setText(checkInfoBean.getInfo().getJldwfzr());
            jsdw.setText(checkInfoBean.getInfo().getJsdw());
            cwzd.setText(checkInfoBean.getInfo().getCwzd());
            ddt.setText(checkInfoBean.getInfo().getDdt());
            dxjz.setText(checkInfoBean.getInfo().getSgdxjz());
            sbgldw.setText(checkInfoBean.getInfo().getSbgldw());
            qtdw.setText(checkInfoBean.getInfo().getQtdw());
            shcs.setText(checkInfoBean.getInfo().getShcs());
            xmzgbm.setText(checkInfoBean.getInfo().getXmzgbm());

            //该计划是已下达计划 恢复按钮不可用
            if (checkInfoBean.getInfo().getLcstep().equals(String.valueOf(StatusUtil.STATIUS_YXD))) {
                hf.setVisibility(View.GONE);
                isRecovery = false;
                isDesorty = true;
            }
            if (checkInfoBean.getInfo().getLcstep().equals(String.valueOf(StatusUtil.STATIUS_YZF))) {
                //该计划是已废除计划 作废按钮不可用
                zf.setVisibility(View.GONE);
                isRecovery = true;
                isDesorty = false;
            }
        }

    }

    @Override
    protected void InitListen() {

        cancel.setOnClickListener(this);
        ckyjh.setOnClickListener(this);
        leftTextView.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.ckyjh:
                //查看日计划对应月计划
                MyApplication.ShowWaitDialog(this);
                Map<String,String> map=new HashMap<>();
                map.put("bh", checkInfoBean.getInfo().getYdbh());

                NetWorkApi.getZsyjhXx(map, new Callback<ShyjhxqBean>() {
                            @Override
                            public void onResponse(Call<ShyjhxqBean> call, Response<ShyjhxqBean> response) {
                                try {
                                    mZsyjhxxBean = response.body();

                                    if (mZsyjhxxBean.getMsg().equals("success")) {
                                        ZsyjhChaKanActivity.ZsyjhxxBean = mZsyjhxxBean;
                                        startActivity(new Intent(ZsrjhChaKanActivity.this, ZsyjhChaKanActivity.class));
                                    }
                                } catch (Exception e) {
                                    MyToast.showFail();
                                }
                                MyApplication.HideWaitDialog();
                            }

                            @Override
                            public void onFailure(Call<ShyjhxqBean> call, Throwable t) {
                                MyToast.showFail();
                            }
                        }
                );

                break;
            case R.id.zf:
                //日计划作废
                if (isDesorty) {
                    showCenterDialog(this, "您确定要作废该计划？", new CenterDialog.CenterClick() {
                        @Override
                        public void confirmListener() {
                            MyApplication.ShowWaitDialog(ZsrjhChaKanActivity.this);
                            setPostDate();
                            MyApplication.ShowWaitDialog(ZsrjhChaKanActivity.this);
                            NetWorkApi.changeZsrjh(mZsjhChangeBean, new Callback<SuccessBean>() {
                                        @Override
                                        public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                            try {
                                                SuccessBean bean = response.body();
                                                if (bean.getMsg().equals("success")) {
                                                    MyToast.showDestorySuccess();
                                                }
                                            } catch (Exception e) {
                                                MyToast.showFail();
                                            }
                                            MyApplication.HideWaitDialog();
                                        }

                                        @Override
                                        public void onFailure(Call<SuccessBean> call, Throwable t) {
                                            MyToast.showFail();
                                        }
                                    }
                            );
                        }

                        @Override
                        public void cancelListener() {

                        }
                    });
                }
                break;
            case R.id.hf:
                //日计划恢复
                if (isRecovery) {
                    showCenterDialog(this, "您确定要恢复该计划？", new CenterDialog.CenterClick() {
                        @Override
                        public void confirmListener() {
                            MyApplication.ShowWaitDialog(ZsrjhChaKanActivity.this);
                            setPostDate();
                            MyApplication.ShowWaitDialog(ZsrjhChaKanActivity.this);
                            NetWorkApi.changeZsrjh(mZsjhChangeBean, new Callback<SuccessBean>() {
                                        @Override
                                        public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                            try {
                                                SuccessBean bean =response.body();
                                                if (bean.getMsg().equals("success")) {
                                                    MyToast.showRecoverySuccess();
                                                }
                                            } catch (Exception e) {
                                                MyToast.showFail();
                                            }
                                            MyApplication.HideWaitDialog();
                                        }

                                        @Override
                                        public void onFailure(Call<SuccessBean> call, Throwable t) {
                                            MyToast.showFail();
                                        }
                                    }
                            );
                        }

                        @Override
                        public void cancelListener() {

                        }
                    });
                }
                break;
            case R.id.leftTextView:
                finish();
                break;
        }
    }

    //设置post数据实体对象
    public void setPostDate() {
        mZsjhChangeBean = new ZsjhChangeBean();
        mZsjhChangeBean.setBh(String.valueOf(checkInfoBean.getInfo().getRjdjhbId()));
        ZsjhChangeBean.LjyyxRjdjhbShdw bean = new ZsjhChangeBean.LjyyxRjdjhbShdw();
        bean.setJdjhbBh(String.valueOf(checkInfoBean.getInfo().getRjdjhbId()));
        bean.setDwId(MyApplication.getmLoginUser().getUserUnitId());
        bean.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
//        bean.setShyj();
        if (checkInfoBean.getInfo().getJsdwid().equals(MyApplication.getmLoginUser().getUserUnitId())) {
            bean.setDwlb("jsdw");
        } else {
            bean.setDwlb("spcs");
        }

        bean.setCjrId(MyApplication.getmLoginUser().getUserDisplayName());
        bean.setShzt(checkInfoBean.getInfo().getShzt());
        bean.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
        bean.setShrname(MyApplication.getmLoginUser().getTrueName());
        bean.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
        List<ZsjhChangeBean.LjyyxRjdjhbShdw> list = new ArrayList<>();
        list.add(bean);
        mZsjhChangeBean.setRjdjhbShdwList(list);

        if (isDesorty) {
            bean.setSftg("ZF");
            bean.setShsx("90");
        }

        if (isRecovery) {
            bean.setSftg("FF");
            bean.setShsx("100");
        }
    }


}
